//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.supplier;

import java.awt.event.*;
import javax.swing.*;
import org.jpgolay.gui.date.DateComboBox;
import org.jpgolay.gui.fixed.*;
import org.jpgolay.me.util.MEDate;
import org.jpgolay.me.util.MEUtility;

// Referenced classes of package org.jpgolay.me.supplier:
//            SupplierCRUD, SupplierAdvanceSearchGUI, Supplier
public class SupplierPanelController {

    private class MouseClickList extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {
            actionAddressView();
        }

        MouseClickList() {
        }
    }

    public SupplierPanelController(SearchBox deliverySearch, TableViewer deliveryTable, JDesktopPane desktop) {
        supplierSearch = null;
        supplierTable = null;
        supplierSearchGui = null;
        dateComboBox = null;
        supplierSearch = deliverySearch;
        supplierTable = deliveryTable;
        this.desktop = desktop;
        date = new MEDate();
        crud = new SupplierCRUD();
        initSearch();
        initTable();
    }

    private void initSearch() {
        supplierSearch.setList(new String[]{
                    "Code", "Raison social", "Ville", "Famille"
                });
        supplierSearch.setVal("");
        supplierSearch.setAdvanceSearch(getSupplierSearchGUI());
        supplierSearch.getButtonRight().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionSearch();
            }
        });
        supplierSearch.getTextField().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionSearch();
            }
        });
    }

    private void initTable() {
        supplierTable.setTypes(new Class[]{
                    java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
                });
        supplierTable.setLastColumnNotEditable(0);
        supplierTable.setCellEditable(false);
        supplierTable.setAlternateColor(MEUtility.SUPPLIER_LINE);
        supplierTable.setColumnNames(new String[]{
                    "CODE", "FOURNISSEUR", "VILLE", "FAMILLE"
                });
        supplierTable.setPreferredColumnSize(new int[]{
                    40, 50, 40, 50
                });
        actionSearch();
    }

    private SupplierAdvanceSearchGUI getSupplierSearchGUI() {
        if (supplierSearchGui == null) {
            supplierSearchGui = new SupplierAdvanceSearchGUI();
            supplierSearchGui.getAddressViewerPanel().clean();
            supplierSearchGui.getAddressViewerPanel().setCompagnyLabel("Aucune coordonn\351es");
        }
        return supplierSearchGui;
    }

    private void actionSearch() {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT id, raison_sociale, ville_com, familles_fournisseurs_id FROM fournisseurs");
        switch (supplierSearch.getSelectedIndex()) {
            case 0: // '\0'
                if (supplierSearch.getVal().equals("")) {
                    sql.append(" ORDER BY id");
                } else {
                    sql.append(" WHERE id LIKE '");
                    sql.append(supplierSearch.getVal());
                    sql.append("%' ORDER BY id");
                }
                break;

            case 1: // '\001'
                if (supplierSearch.getVal().equals("")) {
                    sql.append(" ORDER BY raison_sociale");
                } else {
                    sql.append(" WHERE raison_sociale LIKE '");
                    sql.append(supplierSearch.getVal());
                    sql.append("%' ORDER BY raison_sociale");
                }
                break;

            case 2: // '\002'
                if (supplierSearch.getVal().equals("")) {
                    sql.append(" ORDER BY ville_com");
                } else {
                    sql.append(" WHERE ville_com LIKE '");
                    sql.append(supplierSearch.getVal());
                    sql.append("%' ORDER BY ville_com");
                }
                break;

            case 3: // '\003'
                if (supplierSearch.getVal().equals("")) {
                    sql.append(" ORDER BY familles_fournisseurs_id");
                } else {
                    sql.append(" WHERE familles_fournisseurs_id LIKE '");
                    sql.append(supplierSearch.getVal());
                    sql.append("%' ORDER BY familles_fournisseurs_id");
                }
                break;

            default:
                if (supplierSearch.getVal().equals("")) {
                    sql.append(" ORDER BY id");
                } else {
                    sql.append(" WHERE id LIKE '");
                    sql.append(supplierSearch.getVal());
                    sql.append("%' ORDER BY id");
                }
                break;
        }
        Object data[][] = crud.getDataList(sql.toString());
        if (data != null) {
            supplierTable.setData(data);
            supplierTable.getTable().addMouseListener(new MouseClickList());
        } else {
            supplierTable.setData(getEmptyTable());
        }
    }

    private Object[][] getEmptyTable() {
        Object data[][] = new Object[3][4];
        for (int i = 0; i < data.length; i++) {
            data[i][0] = null;
            if (i == 1) {
                data[i][1] = "AUCUN";
                data[i][2] = "FOURNISSEUR";
            } else {
                data[i][1] = null;
                data[i][2] = null;
            }
            data[i][3] = null;
        }

        return data;
    }

    private void actionAddressView() {
        Supplier supplier = crud.getSupplierByPK((String) supplierTable.getTable().getValueAt(supplierTable.getTable().getSelectedRow(), 0));
        if (supplier != null) {
            getSupplierSearchGUI().getAddressViewerPanel().setAddrLn1Label(supplier.getAdresseComUn());
            getSupplierSearchGUI().getAddressViewerPanel().setAddrLn2Label(supplier.getAdresseComDeux());
            getSupplierSearchGUI().getAddressViewerPanel().setCityLabel(supplier.getVilleCom());
            getSupplierSearchGUI().getAddressViewerPanel().setCompagnyLabel(supplier.getRaisonSociale());
            getSupplierSearchGUI().getAddressViewerPanel().setZipLabel(supplier.getCpCom());
            getSupplierSearchGUI().getAddressViewerPanel().setStatusLabel(supplier.getCivilite());
            if (supplier.getNomPrenomContact() != null && !supplier.getNomPrenomContact().equals("")) {
                getSupplierSearchGUI().getAddressViewerPanel().setInfo1Label("Contact : " + supplier.getNomPrenomContact());
            } else {
                getSupplierSearchGUI().getAddressViewerPanel().setInfo1Label("");
            }
            if (supplier.getTelephone() != null && !supplier.getTelephone().equals("")) {
                getSupplierSearchGUI().getAddressViewerPanel().setInfo2Label("T\351l : " + supplier.getTelephone());
            } else {
                getSupplierSearchGUI().getAddressViewerPanel().setInfo2Label("");
            }
            if (supplier.getFax() != null && !supplier.getFax().equals("")) {
                getSupplierSearchGUI().getAddressViewerPanel().setInfo4Label("Fax : " + supplier.getFax());
            } else {
                getSupplierSearchGUI().getAddressViewerPanel().setInfo4Label("");
            }
            if (supplier.getPortable() != null && !supplier.getPortable().equals("")) {
                getSupplierSearchGUI().getAddressViewerPanel().setInfo3Label("Port : " + supplier.getPortable());
            } else {
                getSupplierSearchGUI().getAddressViewerPanel().setInfo3Label("");
            }
            if (supplier.getEmail() != null && !supplier.getEmail().equals("")) {
                getSupplierSearchGUI().getAddressViewerPanel().setInfo5Label(supplier.getEmail());
            } else {
                getSupplierSearchGUI().getAddressViewerPanel().setInfo5Label("");
            }
        }
    }
    private SearchBox supplierSearch;
    private TableViewer supplierTable;
    private SupplierAdvanceSearchGUI supplierSearchGui;
    private SupplierCRUD crud;
    private JDesktopPane desktop;
    private DateComboBox dateComboBox;
    private MEDate date;
}
